package com.yanze.schoolinformation.view.information.movie;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.example.xrecyclerview.XRecyclerView;
import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.adapter.rv.DoubanMovieAdapter;
import com.yanze.schoolinformation.base.BaseActivity;
import com.yanze.schoolinformation.databinding.ActivityDoubanMovieBinding;
import com.yanze.schoolinformation.model.bean.HotMovieBean;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.viewModule.movie.DoubanMovieViewModel;
import com.yanze.schoolinformation.viewModule.movie.OnMovieLoadListener;

public class DoubanMovieAtivity extends BaseActivity<ActivityDoubanMovieBinding> implements OnMovieLoadListener {

    private int mStart = 0;
    private int mCount = 21;
    private DoubanMovieViewModel topViewModel;
    private DoubanMovieAdapter mDoubanAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_douban_movie);
        setTitle("豆瓣电影Top250");
        topViewModel = ViewModelProviders.of(this).get(DoubanMovieViewModel.class);
        topViewModel = new DoubanMovieViewModel();
        topViewModel.setListener(this);
        initRecyclerView();
        loadDoubanTop250();
    }

    public void initRecyclerView() {
        mDoubanAdapter = new DoubanMovieAdapter(this);
        bindingView.movieRec.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        bindingView.movieRec.setPullRefreshEnabled(false);
        bindingView.movieRec.setLoadingMoreEnabled(true);
        bindingView.movieRec.setAdapter(mDoubanAdapter);
        bindingView.movieRec.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
            }

            @Override
            public void onLoadMore() {
                mStart += mCount;
                loadDoubanTop250();
            }
        });
    }

    public void loadDoubanTop250() {
        topViewModel.getMovie(mStart, mCount);
    }

    @Override
    public void onSuccess(HotMovieBean bean) {
        showContentView();
        if (mStart == 0) {
            if (bean != null
                    && bean.getSubjects() != null
                    && bean.getSubjects().size() > 0) {
                mDoubanAdapter.clear();
                mDoubanAdapter.addAll(bean.getSubjects());
                mDoubanAdapter.notifyDataSetChanged();
            } else {
                bindingView.movieRec.setVisibility(View.GONE);
                bindingView.movieRec.refreshComplete();
                if (mDoubanAdapter.getItemCount() == 0) {
                    showError();
                }
            }
        } else {
            if (bean != null
                    && bean.getSubjects() != null
                    && bean.getSubjects().size() > 0) {
                bindingView.movieRec.refreshComplete();
                mDoubanAdapter.addAll(bean.getSubjects());
                mDoubanAdapter.notifyDataSetChanged();
            } else {
                bindingView.movieRec.noMoreLoading();
            }
        }
    }

    @Override
    public void onFailure(String errmsg) {
        ToastUtils.show(this, errmsg);
        bindingView.movieRec.refreshComplete();
        if (mDoubanAdapter.getItemCount() == 0) {
            showError();
        }
    }

    @Override
    protected void onRefresh() {
        loadDoubanTop250();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        topViewModel.onDestroy();
    }

    /**
     * 电影列表入口
     */
    public static void start(Context context) {
        context.startActivity(new Intent(context, DoubanMovieAtivity.class));
    }
}
